package com.woniuxy.gobang.ui;

import gobang_util.GobangUtil;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

/**
 * 设置模式界面
 * 
 * @author AC
 *
 */
public class SettingStage extends Stage {

	Stage stage = this;
	// 本机ip
	private Label lbMyIP = new Label("我的IP");
	private Label warnMyIP = new Label();
	private TextField txtMyIp = new TextField("127.0.0.1");

	// 本机端口
	private Label lbMyPort = new Label("我的端口");
	private TextField txtMyPort = new TextField("6000");
	private Label warnMyPort = new Label();

	// 对方IP
	private Label lbOtherIP = new Label("对方IP");
	private TextField txtOtherIP = new TextField("127.0.0.1");
	private Label warnOtherIP = new Label();

	// 对方端口
	private Label lbOtherPort = new Label("对方端口");
	private TextField txtOtherPort = new TextField("6500");
	private Label warnOtherPort = new Label();

	public SettingStage() {
		// 为窗体设置标题
		setTitle("设置界面");
		// 设置窗体大小不可调整
		setResizable(false);
		// 创建面板
		Pane pane = new Pane();

		// 我的IP
		lbMyIP.setLayoutX(20);
		lbMyIP.setLayoutY(20);
		txtMyIp.setLayoutX(90);
		txtMyIp.setLayoutY(20);
		warnMyIP.setLayoutX(300);
		warnMyIP.setLayoutY(20);
		pane.getChildren().addAll(lbMyIP, txtMyIp, warnMyIP);

		// 我的端口
		lbMyPort.setLayoutX(20);
		lbMyPort.setLayoutY(50);
		txtMyPort.setLayoutX(90);
		txtMyPort.setLayoutY(50);
		warnMyPort.setLayoutX(300);
		warnMyPort.setLayoutY(50);
		pane.getChildren().addAll(lbMyPort, txtMyPort, warnMyPort);

		// 对方IP
		lbOtherIP.setLayoutX(20);
		lbOtherIP.setLayoutY(80);
		txtOtherIP.setLayoutX(90);
		txtOtherIP.setLayoutY(80);
		warnOtherIP.setLayoutX(300);
		warnOtherIP.setLayoutY(80);
		pane.getChildren().addAll(lbOtherIP, warnOtherIP, txtOtherIP);

		// 对方端口
		lbOtherPort.setLayoutX(20);
		lbOtherPort.setLayoutY(110);
		txtOtherPort.setLayoutX(90);
		txtOtherPort.setLayoutY(110);
		warnOtherPort.setLayoutX(300);
		warnOtherPort.setLayoutY(110);
		pane.getChildren().addAll(warnOtherPort, txtOtherPort, lbOtherPort);

		// 确认按钮
		Button confirmBtn = new Button("确认");
		confirmBtn.setPrefSize(100, 30);
		confirmBtn.setLayoutX(40);
		confirmBtn.setLayoutY(150);
		pane.getChildren().add(confirmBtn);
		// 确认按钮事件
		confirmBtn.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				boolean dataError = false;
				// 获取文本框输入的信息
				// 检查IP是否格式正确
				if (GobangUtil.ipCheck(txtMyIp.getText())) {
					// ip正确
					Global.myIP = txtMyIp.getText();
				} else {
					warnMyIP.setText("ip格式错误");
					dataError = true;
				}

				if (GobangUtil.portCheck(Integer.parseInt(txtMyPort.getText()))) {
					Global.myPort = Integer.parseInt(txtMyPort.getText());
				} else {
					warnMyPort.setText("端口格式错误");
					dataError = true;
				}

				if (GobangUtil.ipCheck(txtOtherIP.getText())) {
					// ip正确
					Global.otherIP = txtOtherIP.getText();
				} else {
					warnOtherIP.setText("ip格式错误");
					dataError = true;
				}

				if (GobangUtil.portCheck(Integer.parseInt(txtOtherPort.getText()))) {
					Global.otherPort = Integer.parseInt(txtOtherPort.getText());
				} else {
					warnOtherPort.setText("端口格式错误");
					dataError = true;
				}

				// 如果校验不通过不执行
				if (!dataError) {
					// 关闭设置窗口
					stage.close();
					// 打开对战模式棋盘
					// 启动服务器线程
					new ServerThread(new FightModeStage()).start();
				}
			}
		});

		// 取消按钮
		Button cancelBtn = new Button("取消");
		cancelBtn.setPrefSize(100, 30);
		cancelBtn.setLayoutX(160);
		cancelBtn.setLayoutY(150);
		pane.getChildren().add(cancelBtn);
		// 取消按钮事件
		cancelBtn.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				stage.close();
			}
		});

		// 控制窗体尺寸
		Scene scene = new Scene(pane, 500, 300);
		// 将场景添加到窗体上
		setScene(scene);
		show();

	}
}
